package com.example.chapter8;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class RedActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_red);
		
		
		Button button = (Button) this.findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), BlueActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
				//startActivity(intent);
				
				
				AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
				PendingIntent pendingIntent = PendingIntent.getActivity(v.getContext(), 0, intent, 1);
				long triggerTime = SystemClock.elapsedRealtime() + 1000 * 5;
				alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerTime, pendingIntent);
			}
		});
		
		button = (Button) this.findViewById(R.id.button2);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int icon = R.drawable.ic_launcher;
				String tickerText = "Hello, World";
				long when = System.currentTimeMillis();
				
				Context context = getApplicationContext();
				Intent intent = new Intent(v.getContext(), RedActivity.class);
				PendingIntent contentIntent = PendingIntent.getActivity(v.getContext(), 0, intent, 0);
				
				Notification notification = new Notification(icon, tickerText, when);
				notification.setLatestEventInfo(v.getContext(), "My Notification", "Hellow, World", contentIntent);
				
				NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
				notificationManager.notify(0, notification);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
